package cn.amoqi.springboot.mybatisplus.result;

import com.alibaba.fastjson.JSON;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.extern.slf4j.Slf4j;

@Slf4j
public class ActionResult<T> {
    private static int STATUS_SUCCEED = 0;//成功
    public static int STATUS_MESSAGE_ERROR = 1;// 前端认为成功，但是需要显示信息
    public static int STATUS_PARAMS_ERROR = 2;// 参数错误
    public static int STATUS_LOGIC_ERROR = 3;// 逻辑错误
    public static int STATUS_EXCEPTION = 9;// 异常了
    private int code = STATUS_SUCCEED;
    private String message;// 为用户展示的信息
    private T data;// 返回结果
    @JsonIgnore
    private Exception exception;// 只提供日志记录
    private String exceptionMsg;
    public ActionResult(int code, T result) {
        this.code = code;
        this.message = message;
        this.data = result;
    }
    public ActionResult(int code, String msg) {
        this.code = code;
        this.message = msg;
    }

    public ActionResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public ActionResult(int code, String message, T data, Exception exception) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.exception = exception;
    }

    public ActionResult(int code, String message, T data, Exception exception, String exceptionMsg) {
        this.code = code;
        this.message = message;
        this.data = data;
        this.exception = exception;
        this.exceptionMsg = exceptionMsg;
    }


    public static <T> ActionResult result(Boolean b){
        if(b){
            return ActionResult.successMsg("操作成功");
        }else{
            return ActionResult.exceptionError("操作失败");
        }
    }

    public static <T> ActionResult success(T obj){
        return new ActionResult(0,"请求成功",obj);
    }
    public static <T> ActionResult successMsg(String msg){
        return new ActionResult(0,msg,null);
    }

    public static <T> ActionResult exception(Exception e){
        log.error(e.getMessage(),e);
        return new ActionResult(STATUS_EXCEPTION,"未知错误！",null,e,e.getMessage());
    }

    public static <T> ActionResult paraError(String msg){
        return new ActionResult(2,msg);
    }

    public static <T> ActionResult messageError(String msg){
        return new ActionResult(1,msg);
    }

    public static <T> ActionResult logicError(String msg){
        return new ActionResult(3,msg);
    }
    public static <T> ActionResult exceptionError(String msg){
        return new ActionResult(9,msg);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getResult() {
        return data;
    }

    public void setResult(T data) {
        this.data = data;
    }

    public Exception getException() {
        return exception;
    }

    public void setException(Exception exception) {
        this.exception = exception;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }

    public String getExceptionMsg() {
        return exceptionMsg;
    }

    public void setExceptionMsg(String exceptionMsg) {
        this.exceptionMsg = exceptionMsg;
    }
}
